﻿using Cinch;
using FR.SAFTAnalyser.Services;
using MEFedMVVM.Common;
using System.IO;

namespace FR.SAFTAnalyser.ViewModels
{
    public class SaftViewModel : WorkspaceViewModel
    {
        IOpenFileService openFileService;

        public SaftViewModel(IOpenFileService ofs)
        {
            this.openFileService = ofs;
            Init();

            this.DisplayName = "SAF-T";
        }

        private void Init()
        {
            SaftFileName = "Por favor, coloque aqui o ficheiro SAFT.";
            PublicKeyFileName = "Por favor, coloque aqui o ficheiro com a chave pública.";
            PrivateKeyFileName = "Por favor, coloque aqui o ficheiro com a chave privada.";

            NotifyPropertyChanged(ObservableHelper.CreateArgs<SaftViewModel>(x => x.SaftFileName));
            NotifyPropertyChanged(ObservableHelper.CreateArgs<SaftViewModel>(x => x.PublicKeyFileName));

            if (!Designer.IsInDesignMode)
            {
                //Commands
                DoValidateHashCommand = new SimpleCommand<object, object>(CanValidateHash, ValidateHash);
                DoValidateSchemaCommand = new SimpleCommand<object, object>(CanValidateSchema, ValidateSchema);
                
                OpenSaftFileCommand = new SimpleCommand<object, object>(CanOpenSaftFile, OpenSaftFile);
                OpenPublicKeyFileCommand = new SimpleCommand<object, object>(CanOpenPublicKeyFile, OpenPublicKeyFile);
                OpenPrivateKeyFileCommand = new SimpleCommand<object, object>(CanOpenPrivateKeyFile, OpenPrivateKeyFile);

                GenerateHashCommand = new SimpleCommand<object, object>(CanGenerateHash, GenerateHash);
            }
        }

        string saftFileName;
        public string SaftFileName
        {
            get
            {
                if (Properties.Settings.Default.RememberLastOpenFiles && File.Exists(Properties.Settings.Default.SAFTFileName) && !saftFileName.Equals(Properties.Settings.Default.SAFTFileName, System.StringComparison.OrdinalIgnoreCase))
                {
                    saftFileName = Properties.Settings.Default.SAFTFileName;
                    SAFT.SaftAnalyser.Instance.SaftFileName = saftFileName;
                    SAFT.SaftAnalyser.Instance.LoadFileAsync();
                }

                return saftFileName;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    saftFileName = value;

                    SAFT.SaftAnalyser.Instance.SaftFileName = saftFileName;
                    SAFT.SaftAnalyser.Instance.LoadFileAsync();

                    if (Properties.Settings.Default.RememberLastOpenFiles && File.Exists(saftFileName))
                    {
                        Properties.Settings.Default.SAFTFileName = saftFileName;
                        Properties.Settings.Default.Save();
                    }

                    NotifyPropertyChanged(ObservableHelper.CreateArgs<SaftViewModel>(x => x.SaftFileName));
                }
            }
        }

        string publicKeyFileName;
        public string PublicKeyFileName
        {
            get
            {
                if (Properties.Settings.Default.RememberLastOpenFiles && File.Exists(Properties.Settings.Default.PublicKeyFileName))
                {
                    publicKeyFileName = Properties.Settings.Default.PublicKeyFileName;
                    SAFT.SaftAnalyser.Instance.PublicKeyFileName = publicKeyFileName;
                }

                return publicKeyFileName;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    publicKeyFileName = value;

                    SAFT.SaftAnalyser.Instance.PublicKeyFileName = publicKeyFileName;

                    if (Properties.Settings.Default.RememberLastOpenFiles && File.Exists(publicKeyFileName))
                    {
                        Properties.Settings.Default.PublicKeyFileName = publicKeyFileName;
                        Properties.Settings.Default.Save();
                    }

                    NotifyPropertyChanged(ObservableHelper.CreateArgs<SaftViewModel>(x => x.PublicKeyFileName));
                }
            }
        }

        string privateKeyFileName;
        public string PrivateKeyFileName
        {
            get
            {
                if (Properties.Settings.Default.RememberLastOpenFiles && File.Exists(Properties.Settings.Default.PrivateKeyFileName))
                {
                    privateKeyFileName = Properties.Settings.Default.PrivateKeyFileName;
                    SAFT.SaftAnalyser.Instance.PrivateKeyFileName = privateKeyFileName;
                }

                return privateKeyFileName;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    privateKeyFileName = value;

                    SAFT.SaftAnalyser.Instance.PrivateKeyFileName = privateKeyFileName;

                    if (Properties.Settings.Default.RememberLastOpenFiles && File.Exists(privateKeyFileName))
                    {
                        Properties.Settings.Default.PrivateKeyFileName = privateKeyFileName;
                        Properties.Settings.Default.Save();
                    }

                    NotifyPropertyChanged(ObservableHelper.CreateArgs<SaftViewModel>(x => x.PrivateKeyFileName));
                }
            }
        }

        #region Commands
        public SimpleCommand<object, object> DoValidateHashCommand { get; private set; }
        private void ValidateHash(object args)
        {
            SAFTAnalyser.SAFT.SaftAnalyser.Instance.ValidateHash();
        }

        private bool CanValidateHash(object args)
        {
            if (File.Exists(this.SaftFileName) && File.Exists(this.PublicKeyFileName))
                return true;
            else
                return false;
        }

        public SimpleCommand<object, object> DoValidateSchemaCommand { get; private set; }
        private void ValidateSchema(object args)
        {
            SAFTAnalyser.SAFT.SaftAnalyser.Instance.ValidateSchema();
        }

        private bool CanValidateSchema(object args)
        {
            if (File.Exists(this.SaftFileName))
                return true;
            else
                return false;
        }

        public SimpleCommand<object, object> OpenSaftFileCommand { get; private set; }
        private void OpenSaftFile(object args)
        {
            if (openFileService != null)
                this.SaftFileName = openFileService.OpenFile("SAFT Files (*.xml)|*.xml");
        }

        private bool CanOpenSaftFile(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> OpenPublicKeyFileCommand { get; private set; }
        private void OpenPublicKeyFile(object args)
        {
            if (openFileService != null)
                this.PublicKeyFileName = openFileService.OpenFile("Public Key files (.txt) | *.txt");
        }

        private bool CanOpenPublicKeyFile(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> OpenPrivateKeyFileCommand { get; private set; }
        private void OpenPrivateKeyFile(object args)
        {
            if (openFileService != null)
                this.PrivateKeyFileName = openFileService.OpenFile("Private Key files (.txt) | *.txt");
        }

        private bool CanOpenPrivateKeyFile(object args)
        {
            return true;
        }

        public SimpleCommand<object, object> GenerateHashCommand { get; private set; }
        private void GenerateHash(object args)
        {
            SAFTAnalyser.SAFT.SaftAnalyser.Instance.GenerateInvoiceHash();
        }

        private bool CanGenerateHash(object args)
        {
            return File.Exists(this.PrivateKeyFileName) && File.Exists(this.SaftFileName);
        }
        #endregion Commands
    }
}
